package com.sunshine.simplehospital.ui.activity

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.viewpager.widget.ViewPager
import com.google.android.material.snackbar.Snackbar
import com.sunshine.simplehospital.R
import com.sunshine.simplehospital.adapter.MainAdapter
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initPermission()
        textView_title.text = "挂号"
        main_viewpager.apply {
            adapter = MainAdapter(supportFragmentManager)
            addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                override fun onPageScrollStateChanged(state: Int) {

                }

                override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
                ) {

                }

                override fun onPageSelected(position: Int) {
                    bnv.menu.getItem(position).isChecked = true
                    setToolBarTitle(position)
                }

            })
        }
        bnv.setOnNavigationItemSelectedListener {
            main_viewpager.currentItem = when(it.itemId){
                R.id.menu_register -> 0
                R.id.menu_goto -> 1
                R.id.menu_note -> 2
                R.id.menu_me -> 3
                else -> 0
            }
            setToolBarTitle(main_viewpager.currentItem)
            true
        }
    }

    private fun setToolBarTitle(position: Int){
        textView_title.text = when(position){
            0 -> "挂号"
            1 -> "导航"
            2 -> "记录"
            3 -> "我"
            else -> "挂号"
        }
    }

    private fun initPermission() {
        if(checkCallingOrSelfPermission("android.permission.ACCESS_COARSE_LOCATION") != PackageManager.PERMISSION_GRANTED||
            checkCallingOrSelfPermission("android.permission.ACCESS_FINE_LOCATION") != PackageManager.PERMISSION_GRANTED||
            checkCallingOrSelfPermission("android.permission.ACCESS_NETWORK_STAT") != PackageManager.PERMISSION_GRANTED||
            checkCallingOrSelfPermission("android.permission.ACCESS_WIFI_STATE") != PackageManager.PERMISSION_GRANTED||
            checkCallingOrSelfPermission("android.permission.CHANGE_WIFI_STATE") != PackageManager.PERMISSION_GRANTED||
            checkCallingOrSelfPermission("android.permission.READ_PHONE_STATE") != PackageManager.PERMISSION_GRANTED||
            checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE") != PackageManager.PERMISSION_GRANTED||
            checkCallingOrSelfPermission("android.permission.ACCESS_LOCATION_EXTRA_COMMANDS") != PackageManager.PERMISSION_GRANTED||
            checkCallingOrSelfPermission("android.permission.BLUETOOTH") != PackageManager.PERMISSION_GRANTED||
            checkCallingOrSelfPermission("android.permission.BLUETOOTH_ADMIN") != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(
                this,
                arrayOf(
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.ACCESS_WIFI_STATE,
                    Manifest.permission.CHANGE_WIFI_STATE,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                    Manifest.permission.BLUETOOTH,
                    Manifest.permission.BLUETOOTH_ADMIN
                ),
                0
            )
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        var count = 0
        grantResults.forEach {
            if (it == PackageManager.PERMISSION_GRANTED) count++
        }
        if (count != grantResults.size){
            Snackbar.make(bnv, "未授予权限某些功能可能会使用异常", Snackbar.LENGTH_SHORT).show()
        }
    }
}
